package cz.muni.fi.hauntedhousesweb.rest;

import com.sun.jersey.api.core.InjectParam;
import cz.muni.fi.hauntedhouses.service.GhostService;
import cz.muni.fi.hauntedhouses.to.GhostTO;
import cz.muni.fi.hauntedhouses.type.Cas;
import java.io.IOException;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

/**
 *
 * @author Andrea Vasekova, 325181
 */
@Path("/ghosts")
public class GhostServiceREST {
    
    @Context // Allows to inject contextual objects, e.g. ServletContext, Request, Response, UriInfo
    UriInfo uriInfo;
    
    @Context
    Request request;
    
    @InjectParam(value="ghostService")
    GhostService ghostService;
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<GhostTO> getGhosts() {
        return (ArrayList<GhostTO>)(ghostService.findAll());
    }
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("{id}")
    public GhostTO get(@PathParam("id") Long id) {
        return ghostService.get(id);
    }
    
    @GET
    @Path("/name/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<GhostTO> findByName(@PathParam("name") String name) {
        return (ArrayList<GhostTO>)(ghostService.findByName(name));
    }
    
    @PUT
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void create(@FormParam("name") String name, @FormParam("power") float power,
                       @FormParam("hauntingFrom") Cas hauntingFrom, @FormParam("hauntingTo") Cas hauntingTo,
                       @FormParam("skillId") Long skillId, @FormParam("description") String description) throws IOException {
        GhostTO ghost = new GhostTO();
        ghost.setName(name);
        ghost.setPower(power);
        if (hauntingFrom != null) {
            ghost.setHauntingFrom(hauntingFrom);
        }
        if (hauntingTo != null) {
            ghost.setHauntingTo(hauntingTo);
        }
        if (description != null) {
            ghost.setDescription(description);
        }
        //TODO skill?
        ghostService.create(ghost);
    }
    
    @POST
    @Path("{id}")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void update(@PathParam("id") Long id, @FormParam("name") String name, @FormParam("power") float power,
                       @FormParam("hauntingFrom") Cas hauntingFrom, @FormParam("hauntingTo") Cas hauntingTo,
                       @FormParam("description") String description) throws IOException {
        GhostTO ghost = ghostService.get(id);
        ghost.setName(name);
        ghost.setPower(power);
        ghost.setDescription(description);
        ghost.setHauntingFrom(hauntingFrom);
        ghost.setHauntingTo(hauntingTo);
        ghostService.update(ghost);
    }
    
    @DELETE
    @Path("{id}")
    public void delete(@PathParam("id") Long id) throws IOException {
        GhostTO ghost = ghostService.get(id);
        if (ghost != null) {
            ghostService.remove(ghost);
        }
    }
}
